﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using IntoNails.DataAccess;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.InteractionRequests;
using IntoNails.Models;
using IntoNails.Modules.TicketManagement.EventAggregators;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.ViewModel;

namespace IntoNails.Modules.TicketManagement.ViewModels
{
    [Export(typeof(ITicketWorkspaceViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class TicketWorkspaceViewModel : NotificationObject, ITicketWorkspaceViewModel
    {
        #region Fields

        private readonly IEventAggregator _eventAggregator;
        private readonly TicketRepository _ticketRepository;

        private ObservableCollection<Ticket> _ticketCollection;
        private Ticket _selectedTicket;

        #endregion

        #region Constructors

        [ImportingConstructor]
        public TicketWorkspaceViewModel(IEventAggregator eventAggregator)
        {
            this._eventAggregator = eventAggregator;

            this._eventAggregator.GetEvent<CreateNewTicketEventAggregator>().Subscribe(OnCreateNewTicketEventSubscribed, ThreadOption.PublisherThread);
            this._eventAggregator.GetEvent<EditTicketEventAggregator>().Subscribe(OnEditTicketEventSubscribed, ThreadOption.UIThread);
            this._eventAggregator.GetEvent<DeleteTicketEventAggregator>().Subscribe(OnDeleteTicketEventSubscribed, ThreadOption.UIThread);

            this.ShowTicketInteractionDialog = new GenericInteractionRequest<Ticket>();
            this.ShowMessageBox = new GenericInteractionRequest<MessageBoxContext>();

            this._ticketRepository = new TicketRepository(GlobalInfoCaches.StartUpPath + @"\Ticket.dat");
            this._ticketRepository.Load();

            this.PopulateTicketList();
        }
		
		#endregion

        #region Interaction Requests

        public GenericInteractionRequest<Ticket> ShowTicketInteractionDialog { get; private set; }

        public GenericInteractionRequest<MessageBoxContext> ShowMessageBox { get; private set; }

        #endregion

        #region Properties

        public ObservableCollection<Ticket> TicketCollection
        {
            get { return this._ticketCollection; }
            set
            {
                this._ticketCollection = value;
                base.RaisePropertyChanged("TicketCollection");
            }
        }

        public Ticket SelectedTicket
        {
            get { return this._selectedTicket; }
            set
            {
                this._selectedTicket = value;
                base.RaisePropertyChanged("SelectedTicket");
            }
        }

        #endregion

        #region Private Methods

        private void PopulateTicketList()
        {
            this.TicketCollection = new ObservableCollection<Ticket>(this._ticketRepository.GetRawItemList());

        }

        #endregion
    }
}
